<?php
namespace app\shop\controller\goods\self;

use app\common\model\shop\self\SelfGoodsCategoryModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;

/**
 * 自有平台分类
 * @Description
 * @package app\shop\controller\goods\self
 * @author wangqinghui
 * @date 2025-01-09 13:54
 */
class Category extends Controller
{
    /**
     * 分类列表
     * @Description
     * @auth true
     * @menu true
     * @author wangqinghui
     * @date 2025-01-09 14:01
     * @return void
     */
    public function index()
    {
        SelfGoodsCategoryModel::mQuery()->layTable(function () {
            $this->title = lang('分类管理');
            $this->shop_account = $this->account_id;

        }, function (QueryHelper $query) {
            $this->check_user = false;
            // 数据列表搜索过滤
            $pid = (int)$this->request->param('pid', 0);
            $query->where('pid', $pid);
            $query->like('name')->equal('account_id');
        });
    }

    protected function _index_page_filter(&$data)
    {
        $is_show_arr = SelfGoodsCategoryModel::getShowArr();
        foreach ($data as &$vo) {
            $vo['is_show_text'] = $is_show_arr[$vo['is_show']] ?? '';
        }
    }

    /**
     * 异步拉取分类
     * @auth true
     * @Description
     * @author wangqinghui
     * @date 2025-01-09 17:07
     * @return void
     */
    public function pullCategory()
    {
        $account_id = $this->account_id;
        if(empty($account_id) || ($account_id <= 0)){
            $this->error('公司信息有误');
        }
        $this->_queue('异步拉取自有平台分类['.$account_id.']', 'selfAttribute category '.$account_id, 0, ['act_user_id' => $this->user_id]);
    }

    /**
     * 获取ozon分类树
     * @Description
     * @author wangqinghui
     * @date 2024-12-10 16:21
     * @return void
     */
    public function getCategoryTree()
    {
        $where = [];
        $account_id = $this->request->get('account_id', 0);
        if(!empty($account_id)){
            $where[] = ['account_id', '=', $account_id];
        }
        $cate_list = SelfGoodsCategoryModel::mk()->field('cate_id,pid,name')->where($where)->select()->toArray();
        $list = DataExtend::arr2tree($cate_list, 'cate_id', 'pid', 'children');
        // 处理分类树
        $this->success('分类树列表', ['list' => $list]);
    }
}